package br.art.late.openenvironment.boids;

import br.art.late.openenvironment.main.OpenEnvironment;
import processing.core.PFont;
import processing.core.PVector;

public class FpsCounter {
	PVector position;
	float size;
	PFont font;
	int digits;
	private boolean change;
	private float fps, time;
	OpenEnvironment parent;
	
	FpsCounter(OpenEnvironment parent) {
		this.parent = parent;
		init(0, 15);
	}

	FpsCounter(int x, int y) {
		init(x, y);
	}

	private void init(int x, int y) {
		change = true;
		position = new PVector(x, y);
		size = 10;
		digits = 1;
		font = parent.createFont("Arial", 1);
	}

	public void display() {
		time = parent.millis();
		if (change && time % 2 == 0) {
			fps = roundTo(parent.frameRate, digits);
			change = false;
		} else if (!change && time % 2 != 0)
			change = true;
		display(fps);
	}

	private void display(float fps) {
		parent.textFont(font, size);
		parent.fill(0);
		parent.text("FPS " + fps, position.x, position.y);
	}

	public float roundTo(float a, int d) {
		return parent.round(a * parent.pow(10, d)) / parent.pow(10, d);
	}
}

